/**
 * Custom ColumnControlButton for the ItemClass table (enables only the hide function, discards the default functions)
 * 
 * @author remo.lemma@lu.unisi.ch
 */

package ch.unisi.inf.projects.java.uom.gui.table.itemclass;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JSplitPane;

import org.jdesktop.swingx.table.ColumnControlButton;
import org.jdesktop.swingx.table.ColumnControlPopup;

import ch.unisi.inf.projects.java.uom.gui.table.UOMItemClassJTable;

public class UOMItemClassColumnControlButton extends ColumnControlButton
{
	private static final long serialVersionUID = -1L;
	
	private static final Dimension hiddenDimension = new Dimension(15,15);
	
	private boolean state;
	
	private static final Dimension normalDimension = new Dimension(250, 0);
	
	
	public UOMItemClassColumnControlButton (final UOMItemClassJTable table)
	{
		super(table);
		state = table.isVisible();
		final List<Action> actions = new ArrayList<Action>();
		actions.add(new AbstractAction("Hide") {
			private static final long serialVersionUID = -1L;
			public void actionPerformed (ActionEvent e) { 
				state = !state; 
				final Component scrollPane = table.getParent().getParent();
				final Component panel = scrollPane.getParent();
				final JSplitPane p = (JSplitPane) panel.getParent();
				Dimension dimension;
				if (state) {
					dimension = normalDimension;
					table.toShow();
				}
				else {
					dimension = hiddenDimension;
					table.toHide();

				}
				resizeNow(table, dimension);
				resizeNow(table.getParent(), dimension);
				resizeNow(scrollPane, dimension);
				if (state) {
			        scrollPane.setPreferredSize(new Dimension((int) normalDimension.getWidth(),1500));
			        scrollPane.setMaximumSize(new Dimension(Short.MAX_VALUE,1500));
			        table.setMaximumSize(new Dimension(Short.MAX_VALUE,1500));
				}
				resizeNow(panel, dimension);
				p.setDividerLocation(-1);
				//p.setEnabled(state);
				p.revalidate();
				p.repaint();
			} });
	
				
		this.clearAll();
		this.getColumnControlPopup().addAdditionalActionItems(actions);
		
	}
	
	private void resizeNow (final Component component, final Dimension dimension)
	{
		component.setMaximumSize(dimension);
		component.setMinimumSize(dimension);
		component.setPreferredSize(dimension);
	}
	
	protected ColumnControlPopup createColumnControlPopup ()
	{
		return new UOMColumnControlPopup();
	}
    
    protected boolean canControl ()
    {
    	return false;
    }
    
    class UOMColumnControlPopup extends ColumnControlButton.DefaultColumnControlPopup
    {
    	public UOMColumnControlPopup ()
    	{
    		super();
    	}
    	
    	protected void addItems (final List<? extends Action> actions)
    	{
    		for (final Action action : actions)
    			addItem(new JCheckBoxMenuItem(action));
    	}
    }
}